USE [HR_DB]
GO
if exists(select * from dbo.sysobjects where id = object_id(N'trig_users_update')   and   OBJECTPROPERTY(id,   N'IsTRIGGER')   =   1)   
  DROP TRIGGER trig_users_update
GO 
-- =============================================
-- Author:张顺琦(lunizhang)
-- Create date: 20090729
-- Description:	用户表的更新表触发器
-- =============================================
CREATE TRIGGER [dbo].[trig_users_update]
	ON [dbo].[users] 
	INSTEAD OF UPDATE
AS
BEGIN
	DECLARE @errors AS int
	SET @errors=0
	DECLARE @id int,@uname varchar(60),@truename varchar(60),@password char(32),@time datetime
	BEGIN TRANSACTION
	SELECT @id=u_id,@uname=u_name,@truename=u_true_name,@password=u_password,@time=u_start_time FROM DELETED
	INSERT INTO users_bak VALUES (@id,@uname,@truename,@password,@time,getDate(),1)
	SET @errors=@errors+@@ERROR
	SELECT @id=u_id,@uname=u_name,@truename=u_true_name,@password=u_password,@time=u_start_time FROM INSERTED
	UPDATE [dbo].[users] SET u_name=@uname,u_true_name=@truename,u_password=@password ,u_start_time=getDate() WHERE u_id=@id
	SET @errors=@errors+@@ERROR
	IF @errors<>0
		ROLLBACK TRANSACTION
	ELSE
		COMMIT TRANSACTION
END

